﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuTouTiaoAssist.Util;

namespace QuTouTiaoAssist
{
    /// <summary>
    /// 登录窗体
    /// </summary>
    public partial class FrmLogin : FrmBase
    {
        /// <summary>
        /// 获取输入的MemberToken值
        /// </summary>
        public string MemberToken { get; private set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public FrmLogin()
        {
            InitializeComponent();
            base.EnableCancelButton();
        }

        /// <summary>
        /// 窗体载入函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmLogin_Load(object sender, EventArgs e)
        {
            UiUtil.EnableTextBoxSelectAll(this.txtMemberToken);
            this.CancelButton = this.btnCancel;
        }

        /// <summary>
        /// “确定”按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOk_Click(object sender, EventArgs e)
        {
            if (OtherUtil.IsNullOrWhitespace(this.txtMemberToken.Text))
            {
                MsgBox.ShowInfo("MemberToken不能为空！");
                return;
            }

            this.MemberToken = this.txtMemberToken.Text.Trim();
            this.DialogResult = DialogResult.OK;
        }

        /// <summary>
        /// “取消”按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        /// <summary>
        /// “历史账号”按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnHistory_Click(object sender, EventArgs e)
        {
            ContextMenuStrip cms = new ContextMenuStrip();

            var list = ConfigUtil.GetUserInfos();
            if (list.Count < 1)
            {
                cms.Items.Add("无", null, null);
            }
            else
            {
                foreach (var info in list)
                {
                    string title = string.Format("{0}(Mobile:{1},Id:{2},TeacherId:{3})",
                        info.NickName, info.Mobile, info.MemberId, info.TeacherId);
                    var menuItem = cms.Items.Add(title, null, (o, args) =>
                    {
                        this.txtMemberToken.Text = (o as ToolStripItem).Tag.ToString();
                    });
                    menuItem.Tag = info.Token;
                }
            }
            cms.Show(this.btnHistory.Parent, new Point(this.btnHistory.Left, this.btnHistory.Bottom));
        }

        private void btnLoginByMobile_Click(object sender, EventArgs e)
        {
            this.Hide();
            var frm = new FrmLoginByMobile();
            frm.Closed += (o, args) =>
            {
                if (frm.DialogResult == DialogResult.Cancel)
                {
                    this.Show();
                }
                else
                {
                    this.MemberToken = frm.MemberToken;
                    this.DialogResult = DialogResult.OK;
                }
            };
            frm.ShowDialog();
        }

        private void btnFindPwd_Click(object sender, EventArgs e)
        {
            this.Hide();
            var frm = new FrmFindPwd();
            frm.Closed += (o, args) =>
            {
                this.Show();
            };
            frm.ShowDialog();
        }
    }
}
